import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * \* Created with IntelliJ IDEA.
 * \* User: Administrator
 * \* Date: 2022/5/25
 * \* Time: 20:16
 * \* To change this template use File | Settings | File Templates.
 * \* Description:
 * \
 */
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //处理用户请求
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        //判定用户名密码是否正确
        //正常来说，判定操作是要放到数据库中进行存取的
        //此处为了简单，我们直接在代码中写死，，假设有效的用户名喝密码是”zhangsan","123
        if("zhangsan".equals(username) && "123".equals(password)){
            //登录成功
            //创建会话，保存必要的身份信息
            HttpSession httpSession = req.getSession(true);
            //往会话中存储键值对，必要的的身份信息
            httpSession.setAttribute("username",username);
            httpSession.setAttribute("count",0);
            resp.sendRedirect("index");
        }else {
            //登录失败
            resp.getWriter().write("login failed");
        }

    }
}
